﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Diagnostics;
using System.Web.Mvc;

namespace Learn.Web.MVC.Code
{
    [ AttributeUsage(AttributeTargets.Class| AttributeTargets.Method,AllowMultiple=true)]
    public class MyActionFilterAttribute:System.Web.Mvc.ActionFilterAttribute
    {
        public string Name { get; set; }

        private void Print()
        {
            var method = new StackFrame(1, true).GetMethod();
            HttpContext.Current.Response.Write(string.Format("{0}--{1}<br/>", this.Name, method.Name));
        }

        public override void OnActionExecuting(System.Web.Mvc.ActionExecutingContext filterContext)
        {
            Print();
            //filterContext.Result = new EmptyResult();
            if (Name == "Filter2")
            {
                //filterContext.Result = new EmptyResult();
                //filterContext.HttpContext.Response.End();
            }
        }

        public override void OnActionExecuted(System.Web.Mvc.ActionExecutedContext filterContext)
        {
            Print();
        }

        public override void OnResultExecuting(System.Web.Mvc.ResultExecutingContext filterContext)
        {            
            Print();
            //filterContext.Cancel = true;
            //filterContext.HttpContext.Response.End();
            //if (this.Name == "Filter2") filterContext.Cancel = true;
        }

        public override void OnResultExecuted(System.Web.Mvc.ResultExecutedContext filterContext)
        {
            Print();
        }
    }
}
